package com.test.down;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.test.down.beans.LiveBean;
import com.test.down.utils.DownLoadInfouTIL;

import java.util.ArrayList;
import java.util.List;

public class JLVideoDownLoadActivity extends AppCompatActivity {

    private List<LiveBean> historyData=new ArrayList<LiveBean>();

    private ImageView iv_novel_back;
    private TextView tv_option;
    private LinearLayout linear_video;
    private LinearLayout linear_video_no_history;
    private Context mContext;

    private boolean showEdit=false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.jl_activity_video_download_history);
        mContext=this;
        initPagerView();
        initPagerData();
        initPagerEvent();
    }

    private  void initPagerView() {
        iv_novel_back=findViewById(R.id.iv_novel_back);
        tv_option=findViewById(R.id.tv_option);
        linear_video=findViewById(R.id.linear_video);
        linear_video_no_history=findViewById(R.id.linear_video_no_history);
        getDownLoadRecord();
    }
    private  void initPagerData() {

    }
    private  void initPagerEvent() {
        iv_novel_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                liveThisActivity();
            }
        });
        tv_option.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               changeEdit();
            }
        });
    }

    /**
     * 获取下载记录数据
     */
    private  void getDownLoadRecord(){
        List<LiveBean> list= DownLoadInfouTIL.getInstance().readVideoHistory();
        if (list != null) {
            historyData.clear();
            historyData.addAll(list);
        }
        checkDataFormat();
    }

    /**
     * 数据校验设置
     */
    private  void checkDataFormat(){
        if(historyData.isEmpty()){
            linear_video_no_history.setVisibility(View.VISIBLE);
        }else{
            initVideosList();
            linear_video_no_history.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //界面再次唤醒之后检验下载记录是否变化
        getDownLoadRecord();
    }

    /**
     * 返回键监听
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            liveThisActivity();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 更新下载列表数据
     */
    private  void initVideosList(){
        linear_video.removeAllViews();
        for(LiveBean mediaInfo:historyData){
            M3u8DownLoadView loadview =new M3u8DownLoadView(this);
            loadview.initMediaInfo(mediaInfo);
            loadview.setOnDeleteItemListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    linear_video.removeView(loadview);
                    if(historyData.isEmpty()){
                        linear_video_no_history.setVisibility(View.VISIBLE);
                    }else{
                        linear_video_no_history.setVisibility(View.GONE);
                    }
                }
            });
            loadview.setShowEdit(showEdit);
            linear_video.addView(loadview);
        }
    }

    /**
     * 设置item的修改状态
     */
    private  void changeEditView(){
        int childes=linear_video.getChildCount();
        for (int i=0;i<childes;i++){
            if(linear_video.getChildAt(i) instanceof M3u8DownLoadView){
                ((M3u8DownLoadView)linear_video.getChildAt(i)).setShowEdit(showEdit);
            }
        }
    }

    /**
     * 离开此activity但是不销毁
     */
    private  void liveThisActivity(){
        if(showEdit){
            changeEdit();
        }
        moveTaskToBack(true);
    }

    /**
     * 改变编辑状态
     */
    private void changeEdit(){
        showEdit=!showEdit;
        if(showEdit){
            tv_option.setText("完成");
        }else{
            tv_option.setText("编辑");
        }
        if(linear_video.getChildCount()==0) {
            return;
        }
        changeEditView();
    }
}
